package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.CateringDao;
import com.wn.dao.HotelinfoDao;
import com.wn.dao.RoutesDao;
import com.wn.dao.ScenicDao;
import com.wn.entity.Catering;
import com.wn.entity.Hotelinfo;
import com.wn.entity.Routes;
import com.wn.entity.Scenic;
import com.wn.service.RoutesService;
import com.wn.utils.BD;
import com.wn.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * (Routes)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:40:41
 */
@Service("routesService")
@Transactional(rollbackFor = Exception.class)
public class RoutesServiceImpl implements RoutesService {
    @Resource
    private RoutesDao routesDao;
    @Autowired
    private ScenicDao scenicDao;
    @Autowired
    private CateringDao cateringDao;
    @Autowired
    private HotelinfoDao hotelinfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Map<String,Object> queryById(String id) {
        Map<String,Object> map = new HashMap<>();
        List<String> scenicId = new ArrayList<>();
        List<String> cateringId = new ArrayList<>();
        List<String> hotelId = new ArrayList<>();
//        List<Scenic> scenic = new ArrayList<>();
//        List<Catering> catering = new ArrayList<>();
//        List<Hotelinfo> hotel = new ArrayList<>();


        //取出路线包含的地点id
        Routes routes = routesDao.queryById(id);
        String[] routesArr = routes.getRoutesarray().split(",");
        for(String s : routesArr){
            if(s.split("_")[1].equals("travel")){
                hotelId.add(s);
            }
            if(s.split("_")[1].equals("scenic")){
                scenicId.add(s);
            }
            if(s.split("_")[1].equals("catering")){
                cateringId.add(s);
            }
        }

        //根据id取出数据
        String[] array = new String[scenicId.size()];
        List<Scenic> scenic= scenicDao.findByIds(scenicId.toArray(array));
        Hotelinfo hotelinfo = hotelinfoDao.queryById(hotelId.get(0));
        Catering catering1 = cateringDao.queryById(cateringId.get(0));

        //map集合封装
        map.put("routes" ,routes);
        map.put("scenic",scenic);
        map.put("hotel",hotelinfo);
        map.put("catering",catering1);
        return map;
    }

    @Override
    public PageInfo<Routes> searchSomeByPage(PageParam<Routes> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Routes> routes = routesDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Routes> routesPageInfo = new PageInfo<Routes>(routes);

            return routesPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Routes> routes = routesDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Routes> routesPageInfo = new PageInfo<>(routes);

            return routesPageInfo;

        }
    }


    /**
     * 新增数据
     *
     * @param routes 实例对象
     * @return 实例对象
     */
    @Override
    public Routes insert(Routes routes) {
        this.routesDao.insert(routes);
        return routes;
    }

    /**
     * 修改数据
     *
     * @param routes 实例对象
     * @return 实例对象
     */
    @Override
    public Integer update(Routes routes) {

        return this.routesDao.update(routes);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.routesDao.deleteById(id) > 0;
    }
}